/**
 * Created by WYH on 17/4/24.
 */
angular.module('tribalc').controller('RentDetailController', [
    '$rootScope', '$scope', 'Api', 'RentProtocolModel', '$stateParams', '$state', 'RentBuckleModel', 'RentPaymentModel', 'RentPlanItemModel','CompanyModel',
    function ($rootScope, $scope, Api, RentProtocolModel, $stateParams, $state, RentBuckleModel, RentPaymentModel, RentPlanItemModel,CompanyModel) {


        $scope.showRentDetail = true;
        $scope.isProperty = true;
        getRentDetail($stateParams.companyId);
        getCompanyDetail($stateParams.companyId)
        // getBuckleDetail($stateParams.companyId);
        getRentPayment($stateParams.companyId);

        $scope.companyId = $stateParams.companyId;

        $scope.goBack = goBack;



        function getRentDetail(companyId) {
            $scope.selectRentProtocol = new RentProtocolModel();
            Api.getRentDeail(companyId, function (result) {
                console.log(result)
                $scope.rentProtocol = initialRentProtocol(result.data);
                $scope.$parent.selectRentProtocol = $scope.rentProtocol;
                $scope.selectRentProtocol=$scope.rentProtocol;
                getPlanItemList($scope.rentProtocol.id);
                getBuckleDetail($scope.rentProtocol.id);
              
            })
        }

        function initialRentProtocol(protocolList) {
            var rentProtocol;
            for (var i = 0; i < protocolList.length; i++) {
                var protocol = protocolList[i];
                if (protocol.status == RentProtocolModel.ProtocolStatus.actived) {
                    rentProtocol = new RentProtocolModel(protocol);
                    break;
                }
            }
            if (!rentProtocol) {
                rentProtocol = new RentProtocolModel();
            }
            return rentProtocol;
        }

        function getBuckleDetail(companyId) {
            Api.getRentBuckle(companyId, function (result) {
                console.log(result)
                $scope.rentBuckle = new RentBuckleModel(result.data);
            })
        }


        function getRentPayment(companyId) {
            Api.getRentPayment(companyId, function (result) {
                if (result.data.length) {
                    
                    $scope.rentPayment = new RentPaymentModel(result.data[0]);
                  

                }
            })
        }
      function getCompanyDetail(companyId){
        Api.getCompanyDetail(companyId, function (result) {
          $scope.companyInfo = new CompanyModel(result.data);
          
        })
      }
    
        function getPlanItemList(rentId) {
            Api.getRentPlanitemList(null, rentId, function (result) {
              
                $rootScope.id = result.data[0].protocolId
                $scope.rentPlanItemList = RentPlanItemModel.setupPlanItemList(result.data);
            })
        }

        function goBack() {
            $state.go('company.managerRents');
        }


    }]);